package com.oneinlet.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * Created by WangZiHe on 2017/11/5
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */

/**
 * Created by YanCheng on 2017/5/11.
 * 相关文档资料 http://docs.spring.io/spring-data/redis/docs/current/reference/html/
 */
@Configuration
//@ConditionalOnClass(JedisConnection::class, RedisOperations::class, Jedis::class)  //配置的条件是存在相关的redis jar包
public class RedisConfig {

    @Value("${com.oneinlet.config.redisConfig.maxTotal}")
    private int maxTotal = 30;
    @Value("${com.oneinlet.config.redisConfig.maxIdle}")
    private int maxIdle = 10;
    @Value("${com.oneinlet.config.redisConfig.maxWaitMillis}")
    private long maxWaitMillis = 10000;
    @Value("${com.oneinlet.config.redisConfig.minIdle}")
    private int minIdle = 1;

    @Value("${com.oneinlet.config.redisConfig.port}")
    private int port = 6379;
    @Value("${com.oneinlet.config.redisConfig.hostName}")
    private String hostName = "localhost";
    @Value("${com.oneinlet.config.redisConfig.password}")
    private String password = "";


    @Bean
    public JedisClientConfiguration getJedisClientConfiguration() {
        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisPoolingClientConfigurationBuilder = (
                JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
        GenericObjectPoolConfig GenericObjectPoolConfig = new GenericObjectPoolConfig();
        GenericObjectPoolConfig.setMaxIdle(maxIdle);
        GenericObjectPoolConfig.setMaxTotal(maxTotal);
        GenericObjectPoolConfig.setMinIdle(maxIdle);
        //保证连接的有效性，即使redis.conf 配置了timeout关闭连接， 也不会自动报错,会尝试自动获取有效连接
        GenericObjectPoolConfig.setTestOnBorrow(true);
        GenericObjectPoolConfig.setTestOnReturn(true);
        return jedisPoolingClientConfigurationBuilder.poolConfig(GenericObjectPoolConfig).build();
    }

    @Bean
    @ConditionalOnMissingBean
    public JedisConnectionFactory getJedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(0);
        redisStandaloneConfiguration.setHostName(hostName);
        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
        redisStandaloneConfiguration.setPort(port);
        return new JedisConnectionFactory(redisStandaloneConfiguration, getJedisClientConfiguration());
    }

    @Bean(value = "stringRedisTemplate")
    public RedisTemplate getStringRedisTemplate() {
        RedisTemplate redisTemplate = new StringRedisTemplate();
        redisTemplate.setConnectionFactory(getJedisConnectionFactory());
        return redisTemplate;
    }

}